package uto.phone.flash.wind.app

import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import uto.phone.flash.wind.R
import uto.phone.flash.wind.base.IActivity
import uto.phone.flash.wind.base.IAdapter
import uto.phone.flash.wind.base.IHolder
import uto.phone.flash.wind.base.findView

open class HomeUserHolder(group: ViewGroup,activity: IActivity?=null,adapter: IAdapter?=null): IHolder(group,activity,adapter){
    lateinit var tvTitle: TextView
    lateinit var rvContent: RecyclerView
    lateinit var dataList: MutableList<UserBean>
    lateinit var itemAdapter: IAdapter

    override fun layoutRes(): Int {
        return R.layout.layout_user_list
    }

    override fun initLayout() {
        tvTitle = findView(R.id.tvTitle)
        rvContent = findView(R.id.rvContent)
        dataList = getData()
        itemAdapter = object: IAdapter(dataList,activity){
            override fun createHolder(parent: ViewGroup, viewType: Int): IHolder {

                return object: IHolder(parent,activity,this){
                    lateinit var ivImage: ImageView
                    lateinit var tvPhone: TextView
                    lateinit var bean: UserBean

                    override fun layoutRes(): Int {
                        return R.layout.layout_user_list_item
                    }

                    override fun initLayout() {
                        ivImage = findView(R.id.ivImage)
                        tvPhone = findView(R.id.tvPhone)
                        bean = UserBean()
                    }

                    override fun bindData(any: Any?) {
                        bean = any as? UserBean ?: bean
                        ivImage.setImageResource(bean.image)
                        tvPhone.text = bean.number
                    }

                    override fun onClick(v: View?) {
                        val intent = Intent(activity?:return,Next2Activity::class.java)
                        intent.putExtra("phone",bean.number)
                        intent.putExtra("image",bean.image)
                        activity?.startActivity(intent)
                    }
                }
            }
        }

        rvContent.adapter = itemAdapter
    }

    private fun getData(): MutableList<UserBean>{
        val list = mutableListOf<UserBean>()
        list.add(UserBean(number = "155-5574-2547",image = R.mipmap.bg_1))
        list.add(UserBean(number = "125-5784-3358",image = R.mipmap.bg_2))
        list.add(UserBean(number = "184-5475-4885",image = R.mipmap.bg_3))
        list.add(UserBean(number = "185-9825-6687",image = R.mipmap.bg_4))
        list.add(UserBean(number = "177-8749-2547",image = R.mipmap.bg_5))
        list.add(UserBean(number = "157-2358-2674",image = R.mipmap.bg_6))
        list.add(UserBean(number = "168-2584-8472",image = R.mipmap.bg_7))
        list.add(UserBean(number = "135-2154-0254",image = R.mipmap.bg_8))
        list.add(UserBean(number = "184-5847-1025",image = R.mipmap.bg_9))
        return list
    }
}